(load "2.63.rkt")
(load "2.64.rkt")

(define (union-set tree1 tree2)
  (define (recur list1 list2)
    (cond ((null? list1) list2)
          ((null? list2) list1)
          ((= (car list1) (car list2))
           (cons (car list1)
                 (recur (cdr list1) (cdr list2))))
          ((< (car list1) (car list2))
           (cons (car list1)
                 (recur (cdr list1) list2)))
          (else
           (cons (car list2)
                 (recur list1 (cdr list2))))))
  (list->tree (recur (tree->list-2 tree1) (tree->list-2 tree2))))

(define (intersection-set tree1 tree2)
  (define (recur list1 list2)
    (cond ((or (null? list1) (null? list2)) '())
          ((= (car list1) (car list2))
           (cons (car list1)
                 (recur (cdr list1) (cdr list2))))
          ((< (car list1) (car list2))
           (recur (cdr list1) list2))
          (else
           (recur list1 (cdr list2)))))
  (list->tree (recur (tree->list-2 tree1) (tree->list-2 tree2))))

(define t1 '(6 (2 (1 () ()) (5 () ())) (7 () (9 () ()))))
(define t2 '(6 (4 (2 () ()) ()) (8 () (10 () ()))))

(display "set1:")
(display (tree->list-2 t1))
(newline)
(display "set2:")
(display (tree->list-2 t2))
(newline)
(display (tree->list-2 (union-set t1 t2)))
(newline)
(display (tree->list-2 (intersection-set t1 t2)))